package com.hzdl.cinema.Service.impl;

import com.hzdl.cinema.Service.UserService;
import com.hzdl.cinema.dao.UserDao;
import com.hzdl.cinema.dao.impl.UserDaoImpl;
import com.hzdl.cinema.domain.User;

import java.util.List;


public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public boolean register(User user) {
       User userExist = userDao.finfByUser(user.getUserName());
        if(userExist != null){
            return false;
        }else {
            userDao.save(user);
            return true;
        }
    }

    @Override
    public User login(User user) {
        User userLogin =  userDao.login(user);
        if (userLogin!=null &&null==userLogin.getUserStatus()){
            userLogin.setUserStatus(0);
        }
        return userLogin;
    }

    @Override
    public boolean update(User user) {
       User upUser = userDao.updateUser(user.getUserName());
       if(upUser==null){
           return false;
       }else {
           userDao.updateSave(user);
           return true;
       }
    }

    @Override
    public List<User> findAll() {
        List<User> userList =  userDao.findAll();
        return userList;
    }

    @Override
    public User findStudioById(int userId) {
        User user = userDao.findUserById(userId);
        return user;
    }

    @Override
    public void update_users(int userId, User user) {
        User  user1 =  userDao.findUser(user.getUserName());
        userDao.update_users(userId,user.getUserName(),user.getUserPassword(),user.getUserEmail());
    }

    @Override
    public boolean addUser(User user) {
        User userAdd  = userDao.findByName(user.getUserName());
        if(userAdd!=null){
            return false;
        }else {
            userDao.addUser(user.getUserName(),user.getUserPassword(),user.getUserEmail(),user.getUserStatus());
        }
        return false;
    }

    @Override
    public void deleteStudioById(int user) {
        userDao.selectUserById(user);

    }

    @Override
    public User searchUserByName(String userName) {
        User user =  userDao.searchUserByName(userName);
        return user;
    }

}
